package cn.com.generaldata.os.service.web;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import cn.com.generaldata.os.constant.FilterConstant;
import cn.com.generaldata.os.paging.Filter;
import cn.com.generaldata.os.paging.FilterRule;
import cn.com.generaldata.os.pojo.Area;
import cn.com.generaldata.os.pojo.Unit;
import cn.com.generaldata.os.pojo.User;
import cn.com.generaldata.os.service.common.AbstractPmService;

@Component
public class PmServiceForWeb extends AbstractPmService{


	/**
	 * 根据企业名称检索企业.
	 */
	public List<Unit> queryCompanyByName(String name, String uid) {

		// 当前登录用户ID
		if (uid != null && uid.length() > 0) {
			User user = userDAO.get(User.class, uid);
			String roleId = user.getRole().getId(); // 用户的角色Id
			Area area = user.getArea(); // 用户的区域

			// 外网的企业和涉海部门，只能给本级的海洋局用户发
			if (roleId.equals("3") || roleId.equals("5")) {
				Filter filter = new Filter();
				filter.addRule(new FilterRule(FilterConstant.EQUAL, "role.id",
						"4")); // 4海洋局用户
				filter.addRule(new FilterRule(FilterConstant.EQUAL, "area.id",
						area.getId()));
				if (name != null && !"".equals(name)) {
					filter.addRule(new FilterRule(FilterConstant.LIKE,
							"unitName", "%" + name + "%"));
				}
				return unitDAO.find(Unit.class, filter, null);
			}

			// 外网的海洋局用户，能给自己上级和自己下级的海洋局用户发，还能给自己本级的企业和涉海部门
			List<Unit> totalList = new ArrayList<Unit>();
			
			//懒得改，这样凑合凑合吧
			if (name != null && name.length() > 0) {
				
				// 上级海洋局用户
				String hql = "select u from Unit u where role.id=4 and area.id = ? and unitName like ?";
				List<Unit> resultList = unitDAO.findByHql(hql,
						area.getParentId(),"%" + name + "%");
				totalList.addAll(resultList);

				// 下级海洋局用户
				hql = "select u from Unit u inner join u.area A where u.role.id=4 and A.parentId = ? and u.unitName like ?";
				resultList = unitDAO.findByHql(hql, area.getId(),"%" + name + "%");
				totalList.addAll(resultList);

				// 本级的企业和涉海部门
				hql = "select u from Unit u where (u.role.id=3 or u.role.id=5) and area.id = ? and u.unitName like ?";
				resultList = unitDAO.findByHql(hql, area.getId(),"%" + name + "%");
				totalList.addAll(resultList);
			} else {
				
				// 上级海洋局用户
				String hql = "select u from Unit u where role.id=4 and area.id = ?";
				List<Unit> resultList = unitDAO.findByHql(hql,
						area.getParentId());
				totalList.addAll(resultList);

				// 下级海洋局用户
				hql = "select u from Unit u inner join u.area A where u.role.id=4 and A.parentId = ?";
				resultList = unitDAO.findByHql(hql, area.getId());
				totalList.addAll(resultList);

				// 本级的企业和涉海部门
				hql = "select u from Unit u where (u.role.id=3 or u.role.id=5) and area.id = ?";
				resultList = unitDAO.findByHql(hql, area.getId());
				totalList.addAll(resultList);
			}

			return totalList;
		} else {
			return new ArrayList<Unit>();
		}
	}

	

	/**
	 * 按企业名称查询的结果数.
	 */
	
	public int countCompanyByName(String name, String uid) {

		// 当前登录用户ID
		if (uid != null && uid.length() > 0) {
			User user = userDAO.get(User.class, uid);
			String roleId = user.getRole().getId(); // 用户的角色Id
			Area area = user.getArea(); // 用户的区域

			// 外网的企业和涉海部门，只能给本级的海洋局用户发
			if (roleId.equals("3") || roleId.equals("5")) {
				Filter filter = new Filter();
				filter.addRule(new FilterRule(FilterConstant.EQUAL, "role.id",
						"4")); // 4海洋局用户
				filter.addRule(new FilterRule(FilterConstant.EQUAL, "area.id",
						area.getId()));
				if (name != null && !"".equals(name)) {
					filter.addRule(new FilterRule(FilterConstant.LIKE,
							"unitName", "%" + name + "%"));
				}
				return unitDAO.count(Unit.class, filter);
			}

			// 外网的海洋局用户，能给自己上级和自己下级的海洋局用户发，还能给自己本级的企业和涉海部门
			int totalCount = 0;
			if (name != null && name.length() > 0) {

				// 上级海洋局用户
				String hql = "select count(*) from Unit u where role.id=4 and area.id = ? and u.unitName like ?";
				int count = unitDAO.countByHql(hql, area.getParentId(), "%" + name + "%");
				totalCount += count;

				// 下级海洋局用户
				hql = "select count(*) from Unit u inner join u.area A where u.role.id=4 and A.parentId = ? and u.unitName like ?";
				count = unitDAO.countByHql(hql, area.getId(), "%" + name + "%");
				totalCount += count;

				// 本级的企业和涉海部门
				hql = "select count(*) from Unit u where (u.role.id=3 or u.role.id=5) and area.id = ? and u.unitName like ?";
				count = unitDAO.countByHql(hql, area.getId(), "%" + name + "%");
				totalCount += count;

			} else {

				// 上级海洋局用户
				String hql = "select count(*) from Unit u where role.id=4 and area.id = ?";
				int count = unitDAO.countByHql(hql, area.getParentId());
				totalCount += count;

				// 下级海洋局用户
				hql = "select count(*) from Unit u inner join u.area A where u.role.id=4 and A.parentId = ?";
				count = unitDAO.countByHql(hql, area.getId());
				totalCount += count;

				// 本级的企业和涉海部门
				hql = "select count(*) from Unit u where (u.role.id=3 or u.role.id=5) and area.id = ?";
				count = unitDAO.countByHql(hql, area.getId());
				totalCount += count;
			}

			return totalCount;
		} else {
			return 0;
		}
	}
}
